﻿$(function () {
    $('.inputdate').datepicker({ dateFormat: 'd MM, yy' });
    $('.inputdate[orgdate]').each(function () {
        $(this).datepicker('setDate', $.datepicker.parseDate('dd-mm-yy', $(this).attr('orgdate')));
    });


    $('.inputtime').keyfilter(function (c) {
        c = $(this).val() + c;
        if (c.length >= 1) {
            if (c[0] != '0' && c[0] != '1' && c[0] != '2')
                return false;
        }
        if (c.length >= 2) {
            var ci = parseInt(c[1]);
            if (ci + '' != c[1]) return false;
            if (ci < 0 || ci > 9)
                return false;
        }
        if (c.length >= 3) {
            if (c[2] != ':')
                return false;
        }
        if (c.length >= 4) {
            var ci = parseInt(c[3]);
            if (ci + '' != c[3]) return false;
            if (ci < 0 || ci > 5)
                return false;
        }
        if (c.length >= 5) {
            var ci = parseInt(c[4]);
            if (ci + '' != c[4]) return false;
            if (ci < 0 || ci > 9)
                return false;
        }
        if (c.length > 5)
            return false;
        return true;
    });

    $('.edit-record-refa').each(function () {
        var $g = $(this).attr('wrap-group-actions');
        var $refs = $('*[wrap-group=' + $g + ']').find('input[type=hidden]').val();
        var $a = $(this).find('a:eq(0)');
        $a.data('href', $a.attr('href'));
        $a.attr('href', $a.attr('href') + '&selected=' + $refs);
    });

    $('a[href*=remove-reference]').click(function (e) {
        e.preventDefault();
        var $params = $(this).attr('href').split('-');
        var $wrapper = $('*[wrap-group=' + $params[4] + ']');

        $wrapper.find('input[type=hidden]').val('');
        var $a = $wrapper.find('.edit-record-refa a:eq(0)');
        $a.attr('href', $a.data('href'));
        $wrapper.find('.record-refs').html('');
        AfterRecordsSet($wrapper.find('.record-refs'));

    });

    $('.record-refs').each(function () {
        AfterRecordsSet($(this));
    });


    $(function () {
        
    });

});

function AfterRecordsSet(recordRef) {
    recordRef.find('.no-records').remove();
    var $ra = recordRef.parent().find('a[href*=remove-reference]');
    $ra.show();
    if (recordRef.find('a').length == 0) {
        recordRef.append('<p class="no-records">Brak wybranych rekordów</p>');
        $ra.hide();
    }
}


window.SetReferences = function (wrapGroup, records) {

    if (!wrapGroup || !records)
        return;

    window.ClosePopup();

    var wrap = $('*[wrap-group=' + wrapGroup + ']');
    wrap.find('.record-refs').html('');

    var cnt = wrap.find('.record-refs');

    var ar = new Array();

    for (var i = 0; i < records.length; i++) {
        $('<a itemid="' + records[i].Id + '" href="' + records[i].Url + '" class="record-ref">' + records[i].Identyficator + '<span></span></a>').appendTo(cnt);
        cnt.append('<br />');
        ar.push(records[i].Id);
    }

    wrap.find('input[type=hidden]').val(ar.join(','));

    var $a = wrap.find('.edit-record-refa a:eq(0)');
    $a.attr('href', $a.data('href') + '&selected=' + ar.join(','));

    AfterRecordsSet(cnt);

}